import dayjs from 'dayjs';
import { postRequest } from './axios';

let csvFileName = '';
let isStarted = false;

//  创建 CSV 文件
export function createCsvInstance(fileName: string) {
    const secondStr = dayjs().format('MM_DD_HH_mm_ss');
    csvFileName = `${fileName}-${secondStr}`;
    postRequest('/createCsvInstance', { fileName: csvFileName })
    isStarted = true;
}

// 写入数据到 CSV 文件
export function addCsvData(data: { app: string, event_key: string, data_json: any }) {
    console.log({ event_key: data.event_key, ...data.data_json });
    if (isStarted && csvFileName) {
        const reqData = {
            target_time: +new Date(),
            ip: "127.0.0.1",
            channel: "5G",
            version: "1.1",
            account: "asdfgh",
            device_id: "L6",
            display: "F1",
            ...data,
        }
        postRequest('/addCsvData', { fileName: csvFileName, data: reqData })
    }
}

// 销毁 CSV 文件
export function destoryCsvInstance() {
    csvFileName = '';
    isStarted = false;
    postRequest('/destoryCsvInstance', {})
}